package com.nacl.upload.controller;

import com.nacl.upload.po.UploadTask;
import com.nacl.upload.service.AsyncUploadService;
import com.nacl.upload.service.MyUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.WebAsyncTask;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: xukang
 * @create: 2025-08-01
 */

@RestController
public class TestUploadController {
    @Autowired
    private AsyncUploadService uploadService;

    @Autowired
    private MyUploadService myUploadService;


    /**
     * @description: 模拟上传
     * @author: xukang
     * @create: 2025/8/5
     */
    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            // 上传文件并创建任务
            UploadTask task = uploadService.upload(file, getCurrentUserId(),(a,b) -> myUploadService.upload(a,b));
            Map<String, Object> result = createResponse(true, "文件上传成功，正在处理");
            result.put("taskId", task.getId());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(createResponse(false, "文件上传失败: " + e.getMessage()));
        }
    }

    /**
     * @description: 获取单个上传结果，仅30分钟有效
     * @author: xukang
     * @create: 2025/8/5
     */
    @GetMapping("/task/{taskId}")
    public ResponseEntity<Map<String, Object>> getTaskStatus(@PathVariable Long taskId) {
        try {
            UploadTask task = uploadService.getTaskStatus(taskId);
            // todo 检查任务是否属于当前用户（实际项目中需要添加此验证）
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("task", task);

            return ResponseEntity.ok(result);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(createResponse(false, e.getMessage()));
        }
    }

    /**
     * @description: 获取某用户所有的上传任务，仅30分钟有效
     * @author: xukang
     * @create: 2025/8/5
     */
    @GetMapping("/tasks")
    public ResponseEntity<Map<String, Object>> getUserTasks() {
        List<UploadTask> tasks = uploadService.getUserTasks(getCurrentUserId());

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("tasks", tasks);

        return ResponseEntity.ok(result);
    }

    private Map<String, Object> createResponse(boolean success, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", message);
        return response;
    }

    private Long getCurrentUserId() {
        return 999L;
    }


}
